﻿namespace Groundwork.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Reflection;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class StaticUtility
    {
        public static string GetEmbeddedData(string filename, string directory)
        {
            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }

            if (string.IsNullOrEmpty(directory))
            {
                throw new ArgumentNullException("directory");
            }

            string retVal = string.Empty;
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            string assemblyName = executingAssembly.GetName().FullName.Split(',')[0];
            string path = assemblyName + "." + directory + "." + filename;

            string[] resources = executingAssembly.GetManifestResourceNames();

            if (!string.IsNullOrWhiteSpace(filename) && resources.Contains(path))
            {
                using (Stream stream = executingAssembly.GetManifestResourceStream(path))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        retVal = reader.ReadToEnd();
                    }
                }
            }

            return retVal;
        }

        /// <summary>
        /// Builds a path in the file system from the path given as input
        /// </summary>
        /// <param name="path">path to build</param>
        /// <returns>path that was built</returns>
        public static string BuildPath(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            string drive = path.Substring(0, path.IndexOf(":") + 1);
            string tmp = path.Substring(0, path.IndexOf(":") + 1);
            path = path.Replace(tmp, string.Empty);
            if (!Directory.Exists(path))
            {
                Array.ForEach(
                    path.Split(
                    Convert.ToChar(92)),
                    p =>
                    {
                        tmp += "\\" + p;
                        Directory.CreateDirectory(tmp);
                    });
            }

            if (!path.EndsWith(@"\"))
            {
                path += @"\";
            }

            return drive + path;
        }

        /// <summary>
        /// Attempts to convert 1 value to another
        /// </summary>
        /// <typeparam name="T">type to convert to</typeparam>
        /// <param name="value">value to use as a source</param>
        /// <returns>converted value</returns>
        public static T ConvertTo<T>(object value) where T : IConvertible
        {
            if (value != null)
            {
                return (T)Convert.ChangeType(value, typeof(T));
            }

            return default(T);
        }
    }
}
